<template>
  <div class="app-container">
    <!-- 查询功能框 -->
    <div class="filter-container">
      <template>
        <el-tag type="success" style="margin-left: 20px;margin-right: 30px">{{ dep.depName }}</el-tag>
      </template>
      <template>
        <el-input
          v-model="pageParam.model.patientName"
          style="width: 200px;"
          placeholder="姓名查询..."
          class="filter-item"
          @keyup.enter.native="getListByPage"
          @input="getListByPage"
        />
        <el-select v-model="pageParam.model.applyStatus" placeholder="请选择申请状态" @change="getListByPage">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </template>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click=""
      >添加
      </el-button>
    </div>
    <!-- 数据表格 -->
    <el-table
      :data="patientInfoList"
      fit
      highlight-current-row
      style="width: 100%;"
      @expand-change=""
    >

      <el-table-column class="filter-item" type="expand">
        <template slot-scope="{row}">
          <el-form label-position="left" inline class="demo-table-expand">
            <!-- 行数据详情字段 添加了表格-->
            <el-table :data="row.doctorAdviceList">
              <!-- 子表格 -->
              <el-table-column type="expand">
                <template slot-scope="props">
                  <el-form label-position="left" inline class="demo-table-expand">

                    <el-col :span="20">
                      <el-card shadow="hover">
                        <el-form-item label="主治医生">
                          <el-tag v-if="props.row.doctorName !=null" type="success">{{ props.row.doctorName }}</el-tag>
                          <el-tag v-else type="info">{{ 尚未安排 }}</el-tag>
                        </el-form-item>

                        <el-form-item label="床位护士">
                          <el-tag v-if="props.row.nurseName !=null" type="warning">{{ props.row.nurseName }}</el-tag>
                          <el-tag v-else type="info">{{ 尚未安排 }}</el-tag>
                        </el-form-item>

                        <el-form-item label="饮食类别">
                          <span>{{ props.row.dietType }}</span>
                        </el-form-item>

                        <el-form-item label="医嘱详情">
                          <span>{{ props.row.treatment }}</span>
                        </el-form-item>
                      </el-card>
                    </el-col>

                  </el-form>
                </template>
              </el-table-column>
              <!-- 子表格列名 -->

              <el-table-column prop="id" label="医嘱编号"/>
              <el-table-column prop="nursingRoutine" label="护理类型"/>
              <el-table-column prop="nursingLevel" label="护理级别"/>
              <el-table-column prop="treatmentStarttime" label="开始时间"/>

              <el-table-column prop="treatmentEndtime" label="结束时间"/>

              <el-table-column fit label="操作">

                <template slot-scope="scope">
                  <el-button
                    v-if="scope.row.treatmentEndtime==null"
                    size="mini"
                    type="success"
                    :disabled="executeAdviceBtn(scope.row)"
                    @click="executeAdvice(scope.row)"
                    v-text="title"
                  ></el-button>
                  <el-button
                    v-else
                    size="mini"
                    type="info"
                    disabled
                  >时间结束
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-form>
        </template>
      </el-table-column>

      <!-- 表格列名 -->

      <el-table-column type="index" label="序号"/>
      <el-table-column prop="appNo" label="住院编号"/>
      <el-table-column prop="patientName" label="患者姓名"/>
      <el-table-column prop="tellphone" label="联系方式"/>

      <el-table-column prop="inTime" label="住院时间"/>

      <el-table-column prop="applyStatus" label="申请状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.applyStatus==0">申请中</el-tag>
          <el-tag v-if="scope.row.applyStatus==1" type="success">申请成功</el-tag>
          <el-tag v-if="scope.row.applyStatus==2" type="danger">申请失败</el-tag>
          <el-tag v-if="scope.row.applyStatus==3" type="info">已出院</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="bedId" label="床位编号">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.bedId==null" type="danger">未分配</el-tag>
          <el-tag v-if="scope.row.bedId!=null" type="success">
            {{ dep.depName + "-" + scope.row.wardNo + "-" + scope.row.bedId }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getListByPage"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination/index'
import {
  getCurrentUserDep,
  getAllApplyPatientInfo,
  getAllApplicantInfoByAdvice
} from '@/api/application/application'

import {
  getDocAdviceByPatientId,
  updateAdvice,
  insertAdviceAction,
  selectAdviceAction
} from '@/api/doctorAdvice/doctorAdvice'
import {mapGetters} from "vuex";

export default {
  name: 'AdviceExecute',
  // 组件声明
  components: {
    Pagination
  },
  computed: {
    ...mapGetters([
      'roles'
    ])
  },
  data() {
    return {
      title: '执行医嘱',
      options: [{
        value: '',
        label: '显示全部'
      }, {
        value: '0',
        label: '申请中'
      }, {
        value: '1',
        label: '申请通过'
      }, {
        value: '2',
        label: '申请失败'
      }, {
        value: '3',
        label: '已住院'
      }, {
        value: '4',
        label: '已出院'
      }
      ],
      retrunTime: '',
      adviceEndTime: '',
      actionTimea: 0,
      executeShowBtn: true,
      dialogVisible: false,
      patientInfoList: [],
      docAdviceAction: {
        doctorAdviceId: '',
        doctorActionId: '',
        nurseActionId: '',
        actionTime: '',
        doctorIsAction: '',
        nurseIsAction: 1,
        status: 0
      },
      dep: {
        depName:'',
        id: ''
      },
      docAdvice: {
        treatmentEndtime: ''
      },
      docAdviceList: [],
      total: 0,
      pageParam: {
        pageNum: 1,
        pageSize: 6,
        model: {
          applyStatus: '',
          depId: '',
          patientName: ''
        },
        orderParams: [
          ''
        ],
        superSearchKeyWord: ''
      },
      listQuery: { // 分页 排序  查询参数
        pageNum: 1,
        pageSize: 6
      }
    }
  },
  created() {
    this.getCurrentDep()
    this.retrunTime = new Date().getTime()
    this. getListByPage()
  },
  methods: {
    executeAdvice(obj) {
      if (obj.status != 2) {
        obj.status = 1
        updateAdvice(obj).then(res => {
          this.docAdviceAction.doctorAdviceId = obj.id
          this.docAdviceAction.doctorActionId = obj.doctorId
          this.docAdviceAction.nurseActionId = obj.nurseId
          const now = new Date()
          this.docAdviceAction.actionTime = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate() +
            ' ' + now.toLocaleTimeString('chinese', {hour12: false})
          this.docAdviceAction.nurseIsAction = 1
          this.docAdviceAction.status = 0
          insertAdviceAction(this.docAdviceAction).then(res => {
            this.getListByPage()
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        })
      } else {
        this.$notify({
          title: '失败',
          message: '数据状态异常',
          type: 'warning',
          duration: 2000
        })
      }
    },
    setEndTimeAdviceOne() {
      this.docAdvice.treatmentEndtime = this.adviceEndTime
      updateAdvice(this.docAdvice).then(res => {
        this.$notify({
          title: '成功',
          message: '设置成功',
          type: 'success',
          duration: 2000
        })
        this.dialogVisible = false
      })
    },
    executeAdviceBtn(obj) {
      const startTime = new Date(obj.treatmentStarttime)
      const nowTime = new Date().getTime()
      if (nowTime > startTime) {
        selectAdviceAction(obj.id).then(res => {
          if (res.content != null) {
            this.actionTimea = Date.parse(res.content.actionTime)
          }
        })
        if (Math.floor(nowTime / 8640000) !== Math.floor(this.actionTimea / 8640000)) {
          this.title = '今日未执行'
          return false
        } else {
          this.title = '今日已执行'
        }
      }
      return true
    },
    showPatientAdviceDetails(row, rowList) {
      // alert(123);
    },
    showPatientAdvice(row, rowList) {
      getDocAdviceByPatientId(row.patientId).then(res => {
        this.docAdviceList = res.content
        this.retrunTime = 0
      })
    },
    getListByPage() {
      this.pageParam.pageNum = this.listQuery.pageNum
      this.pageParam.pageSize = this.listQuery.pageSize
      this.pageParam.model.depId = this.dep.id
      // 加载申请列表
      getAllApplicantInfoByAdvice(this.pageParam).then(res => {
        this.patientInfoList = res.content.list
      }).catch(err => {
        console.log(err)
      })
    },
    getCurrentDep() {
      if (this.roles.includes('admin')) {
        return
      }
      getCurrentUserDep().then(res => {
        this.dep = res.content
        this.pageParam.model.depId = this.dep.id
      })
    }
  }
}
</script>
<style scoped>
</style>
